{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Reveal Cards In Increasing Order"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #queue #array #sorting #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #队列 #数组 #排序 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: deckRevealedIncreasing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #按递增顺序显示卡牌"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>牌组中的每张卡牌都对应有一个唯一的整数。你可以按你想要的顺序对这套卡片进行排序。</p>\n",
    "\n",
    "<p>最初，这些卡牌在牌组里是正面朝下的（即，未显示状态）。</p>\n",
    "\n",
    "<p>现在，重复执行以下步骤，直到显示所有卡牌为止：</p>\n",
    "\n",
    "<ol>\n",
    "\t<li>从牌组顶部抽一张牌，显示它，然后将其从牌组中移出。</li>\n",
    "\t<li>如果牌组中仍有牌，则将下一张处于牌组顶部的牌放在牌组的底部。</li>\n",
    "\t<li>如果仍有未显示的牌，那么返回步骤 1。否则，停止行动。</li>\n",
    "</ol>\n",
    "\n",
    "<p>返回能以<strong>递增顺序</strong>显示卡牌的牌组顺序。</p>\n",
    "\n",
    "<p>答案中的第一张牌被认为处于牌堆顶部。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>[17,13,11,2,3,5,7]\n",
    "<strong>输出：</strong>[2,13,3,11,5,17,7]\n",
    "<strong>解释：\n",
    "</strong>我们得到的牌组顺序为 [17,13,11,2,3,5,7]（这个顺序不重要），然后将其重新排序。\n",
    "重新排序后，牌组以 [2,13,3,11,5,17,7] 开始，其中 2 位于牌组的顶部。\n",
    "我们显示 2，然后将 13 移到底部。牌组现在是 [3,11,5,17,7,13]。\n",
    "我们显示 3，并将 11 移到底部。牌组现在是 [5,17,7,13,11]。\n",
    "我们显示 5，然后将 17 移到底部。牌组现在是 [7,13,11,17]。\n",
    "我们显示 7，并将 13 移到底部。牌组现在是 [11,17,13]。\n",
    "我们显示 11，然后将 17 移到底部。牌组现在是 [13,17]。\n",
    "我们展示 13，然后将 17 移到底部。牌组现在是 [17]。\n",
    "我们显示 17。\n",
    "由于所有卡片都是按递增顺序排列显示的，所以答案是正确的。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ol>\n",
    "\t<li><code>1 &lt;= A.length &lt;= 1000</code></li>\n",
    "\t<li><code>1 &lt;= A[i] &lt;= 10^6</code></li>\n",
    "\t<li>对于所有的&nbsp;<code>i != j</code>，<code>A[i] != A[j]</code></li>\n",
    "</ol>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [reveal-cards-in-increasing-order](https://leetcode.cn/problems/reveal-cards-in-increasing-order/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [reveal-cards-in-increasing-order](https://leetcode.cn/problems/reveal-cards-in-increasing-order/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[17,13,11,2,3,5,7]', '[1,1000]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import deque\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        n = deque(range(len(deck)))\n",
    "        m = [0]*len(deck)\n",
    "        ans = 0\n",
    "        # 把模拟的结果赋给m\n",
    "        while ans < len(deck):\n",
    "            m[ans] = n[0]\n",
    "            n.popleft()\n",
    "            n.rotate(-1)\n",
    "            ans += 1\n",
    "        deck.sort()\n",
    "        # 接下来把位次和有序列表一一对应\n",
    "        c = [0]*len(deck)\n",
    "        for i in range(len(deck)):\n",
    "            c[m[i]] = deck[i] \n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        LEN = len(deck)\n",
    "        index = collections.deque(range(LEN)) # [0 1 2 3 4...]\n",
    "        \n",
    "        ans = [None] * LEN\n",
    "        for card in sorted(deck):\n",
    "            ans[index.popleft()] = card\n",
    "            if index:\n",
    "                index.append(index.popleft())\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        n = len(deck)\n",
    "        deck.sort()\n",
    "        res = [0]*n\n",
    "        idx = 0\n",
    "\n",
    "        for i in range(n):\n",
    "            if i > 0:\n",
    "                for _ in range(2):\n",
    "                    idx = (idx+1) % n\n",
    "                    while res[idx] != 0:\n",
    "                        idx = (idx+1) % n\n",
    "            res[idx] = deck[i]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "      deck.sort(reverse=True)\n",
    "      ans = [deck[0]]\n",
    "      i = 1\n",
    "      while len(ans) < len(deck):\n",
    "        ans = [ans[-1]] + ans\n",
    "        ans.pop()\n",
    "        ans = [deck[i]] + ans\n",
    "        i += 1\n",
    "      return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        p = sorted(deck)\n",
    "\n",
    "        q = [p[-1]]\n",
    "\n",
    "        for i in range(1, len(p)):\n",
    "            q=[q[-1]]+q[:-1]\n",
    "            q=[p[-(i+1)]]+q\n",
    "\n",
    "\n",
    "        return q\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import deque\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck):\n",
    "        deck.sort()\n",
    "        n = len(deck)\n",
    "        res = [0]*n\n",
    "        d =deque(range(n))\n",
    "        for i in deck:\n",
    "            res[d.popleft()] = i\n",
    "            if d:\n",
    "                d.append(d.popleft())\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        a = list(range(len(deck)))\n",
    "        ans = [0]*len(deck)\n",
    "        for i in range(len(deck)-1):\n",
    "            a.append(a[i+1])\n",
    "            del a[i+1]\n",
    "        deck.sort()\n",
    "        for i in range(len(deck)):\n",
    "            ans[a[i]] = deck[i]\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        n = len(deck)\n",
    "        index = collections.deque(range(n))\n",
    "        ans = [None] * n \n",
    "        for card in sorted(deck):\n",
    "            ans[index.popleft()] = card\n",
    "            if index:\n",
    "                index.append(index.popleft())\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort(reverse = True)\n",
    "        stack = collections.deque()\n",
    "        for val in deck:\n",
    "            if not stack: stack.append(val)\n",
    "            else:\n",
    "                stack.appendleft(stack.pop())\n",
    "                stack.appendleft(val)\n",
    "        return [x for x in stack]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort()\n",
    "        res = []\n",
    "        for _ in range(len(deck)):\n",
    "            res.insert(0, deck.pop())\n",
    "            res.insert(0, res.pop())\n",
    "        res.append(res.pop(0))\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        def reorder(current_deck):\n",
    "            current_deck.sort()\n",
    "            if len(current_deck) <=2:\n",
    "                return current_deck\n",
    "            \n",
    "            len_current = len(current_deck)\n",
    "            if len_current % 2:\n",
    "                one_half =  current_deck[:len_current // 2 + 1]\n",
    "                next_half =  current_deck[len_current // 2:]\n",
    "                next_half = reorder(next_half)\n",
    "                next_half = next_half[1:]\n",
    "                # print(1, len_current % 2, one_half, next_half)\n",
    "            else:\n",
    "                one_half =  current_deck[:len_current // 2]\n",
    "                next_half =  current_deck[len_current // 2:]\n",
    "                next_half = reorder(next_half)\n",
    "                \n",
    "                # print(2 ,len_current % 2, one_half, next_half)\n",
    "\n",
    "            \n",
    "            \n",
    "            new_desk = []\n",
    "            for i in range(len_current // 2):\n",
    "                new_desk.append(one_half[i])\n",
    "                new_desk.append(next_half[i])\n",
    "            \n",
    "            if len_current % 2:\n",
    "                new_desk.append(one_half[-1])\n",
    "            \n",
    "            return new_desk\n",
    "        \n",
    "        return reorder(deck)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort()\n",
    "        ret = [0] * len(deck)\n",
    "        index = deque(range(len(deck)))\n",
    "        for i in sorted(deck):\n",
    "            ret[index.popleft()] = i \n",
    "            if index:\n",
    "                index.append(index.popleft())\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "      deck.sort(reverse=True)\n",
    "      ans = [deck[0]]\n",
    "      i = 1\n",
    "      while len(ans) < len(deck):\n",
    "        ans = [ans[-1]] + ans\n",
    "        ans.pop()\n",
    "        ans = [deck[i]] + ans\n",
    "        i += 1\n",
    "      return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        LEN = len(deck)\n",
    "        index = collections.deque(range(LEN)) # [0 1 2 3 4...]\n",
    "        \n",
    "        ans = [None] * LEN\n",
    "        for card in sorted(deck):\n",
    "            ans[index.popleft()] = card\n",
    "            if index:\n",
    "                index.append(index.popleft())\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort()\n",
    "        s_sort = deck\n",
    "        s_result = []\n",
    "        while len(s_sort)>0:\n",
    "\n",
    "            s_result.append(s_sort[-1])\n",
    "            s_sort.pop()\n",
    "            s_result.insert(-1,s_result[0])\n",
    "            s_result.pop(0)\n",
    "        \n",
    "        s_result.reverse()\n",
    "\n",
    "        return s_result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort(reverse = True)\n",
    "        stack = collections.deque()\n",
    "        for val in deck:\n",
    "            if not stack: stack.append(val)\n",
    "            else:\n",
    "                stack.appendleft(stack.pop())\n",
    "                stack.appendleft(val)\n",
    "        return [x for x in stack]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import deque\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        n = len(deck)\n",
    "        # 创建一个从小到大排序的数组\n",
    "        sorted_deck = sorted(deck)\n",
    "        \n",
    "        # 创建一个双端队列\n",
    "        q = deque()\n",
    "        # 从最大的数开始，逆向模拟揭示卡牌的过程\n",
    "        for i in range(n - 1, -1, -1):\n",
    "            if q:\n",
    "                # 将队列的最后一个元素移到最前面\n",
    "                q.appendleft(q.pop())\n",
    "            # 将当前元素放入队列的最前面\n",
    "            q.appendleft(sorted_deck[i])\n",
    "\n",
    "        # 将队列转换成列表\n",
    "        return list(q)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "                    deck.sort()\n",
    "                    q = deque()\n",
    "                    for i in range(len(deck)):\n",
    "                        q.append(i)\n",
    "                    ans = [0] * len(deck)\n",
    "                    for card in deck:\n",
    "                        ans[q.popleft()] = card\n",
    "                        if q:\n",
    "                            q.append(q.popleft())\n",
    "                    return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort()\n",
    "        s_sort = deck\n",
    "        s_result = []\n",
    "        while len(s_sort)>0:\n",
    "\n",
    "            s_result.append(s_sort[-1])\n",
    "            s_sort.pop()\n",
    "            s_result.insert(-1,s_result[0])\n",
    "            s_result.pop(0)\n",
    "        \n",
    "        s_result.reverse()\n",
    "\n",
    "        return s_result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort(reverse=True)\n",
    "        ans = deque([deck[0]])\n",
    "        for i in range(1, len(deck)):\n",
    "            if ans:\n",
    "                front = ans.popleft()\n",
    "                ans.append(front)\n",
    "            ans.append(deck[i])\n",
    "        ans.reverse()\n",
    "        return list(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        # length, index = len(deck), 0\n",
    "        # deck_mapping_index = dict()\n",
    "        # queue = deque(deck)\n",
    "        # while queue:\n",
    "        #     deck_mapping_index[queue.popleft()] = index\n",
    "        #     index += 1\n",
    "        #     if queue:\n",
    "        #         queue.append(queue.popleft())\n",
    "        # temp = sorted(deck)\n",
    "        # return [temp[deck_mapping_index[deck[i]]] for i in range(length)]\n",
    "        n = len(deck)\n",
    "        index = collections.deque(range(n))\n",
    "        ans = [None] * n\n",
    "\n",
    "        for card in sorted(deck):\n",
    "            ans[index.popleft()] = card\n",
    "            if index:\n",
    "                index.append(index.popleft())\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        n = len(deck)\n",
    "        q = deque(list(range(n)))\n",
    "        ans = [-1] * n\n",
    "        for c in sorted(deck):\n",
    "            ans[q.popleft()] = c\n",
    "            if q:\n",
    "                q.append(q.popleft())\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        from  collections import  deque\n",
    "        p = sorted(deck)\n",
    "\n",
    "        q = [p[-1]]\n",
    "\n",
    "        for i in range(1, len(p)):\n",
    "            q=q[1:]+[q[0]]\n",
    "            q.append(p[-(i+1)])\n",
    "\n",
    "\n",
    "        return q[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        N = len(deck)\n",
    "        index = collections.deque(range(N))\n",
    "        ans = [None] * N\n",
    "\n",
    "        for card in sorted(deck):\n",
    "            ans[index.popleft()] = card\n",
    "            if index:\n",
    "                index.append(index.popleft())\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        index1 = list(range(len(deck)))\n",
    "        end = []\n",
    "        while index1:\n",
    "            end.append(index1.pop(0))\n",
    "            if index1:\n",
    "                index1.append(index1.pop(0))\n",
    "        save = sorted(deck)\n",
    "        ans = [1]*len(deck)\n",
    "        count = 0\n",
    "        for i in end:\n",
    "            ans[i]=save[count]\n",
    "            count += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort(reverse=True)\n",
    "        from collections import deque\n",
    "        q=deque()\n",
    "        # print(deck)\n",
    "        for x in deck:\n",
    "            q.rotate()\n",
    "            q.appendleft(x)\n",
    "            # print(q)\n",
    "        return list(q)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def deckRevealedIncreasing(self, deck):\n",
    "        N = len(deck)\n",
    "        index = collections.deque(range(N))\n",
    "        ans = [None] * N\n",
    "\n",
    "        for card in sorted(deck):\n",
    "            ans[index.popleft()] = card\n",
    "            if index:\n",
    "                index.append(index.popleft())\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        \n",
    "        p = sorted(deck)\n",
    "\n",
    "        q = [p[-1]]\n",
    "\n",
    "        for i in range(1, len(p)):\n",
    "            q=q[1:]+[q[0]]\n",
    "            q.append(p[-(i+1)])\n",
    "\n",
    "\n",
    "        return q[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\r\n",
    "        # 1. 模拟\r\n",
    "        ans = []\r\n",
    "        deck.sort()\r\n",
    "        while deck:\r\n",
    "            ans.insert(0, deck.pop())\r\n",
    "            if deck:\r\n",
    "                ans.insert(0, ans.pop())\r\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort()\n",
    "        n = len(deck)\n",
    "        ans = [0] * n\n",
    "        cur = 0\n",
    "        cnt = 2\n",
    "        for x in deck:\n",
    "            while cnt < 2:\n",
    "                cur = (cur + 1) % n\n",
    "                if ans[cur] == 0:\n",
    "                    cnt += 1\n",
    "            cnt = 0\n",
    "            ans[cur] = x\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        # 模拟法\n",
    "        s_deck = sorted(deck)\n",
    "        s = [i for i in range(len(deck))]\n",
    "        re = [0] * len(deck)\n",
    "        for card in s_deck:\n",
    "            re[s.pop(0)] = card\n",
    "            if s:\n",
    "                s.append(s.pop(0))\n",
    "        return re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort()\n",
    "        ret = [0] * len(deck)\n",
    "        index = deque(range(len(deck)))\n",
    "        for i in sorted(deck):\n",
    "            ret[index.popleft()] = i \n",
    "            if index:\n",
    "                index.append(index.popleft())\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import deque\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort()\n",
    "        ret = [0] * len(deck)\n",
    "        index = deque(range(len(deck)))\n",
    "        for i in sorted(deck):\n",
    "            ret[index.popleft()] = i \n",
    "            if index:\n",
    "                index.append(index.popleft())\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort()\n",
    "        n = deck.__len__()\n",
    "        res = []\n",
    "        for i in range(n):\n",
    "            res.insert(0, deck.pop())\n",
    "            res.insert(0, res.pop())\n",
    "            # print(res)\n",
    "        res.append(res.pop(0))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        \n",
    "        out = deck.copy()\n",
    "        deck.sort()\n",
    "        stack = [i for i in range(len(out))]\n",
    "        top = 0\n",
    "        i = 0        \n",
    "        while i < len(deck):\n",
    "            pos = stack[top]\n",
    "            out[pos] = deck[i]\n",
    "            if top + 1 < len(stack):\n",
    "                stack.append(stack[top+1])\n",
    "            top += 2\n",
    "            i += 1\n",
    "        return out\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        # 1.初始化一个从0到n的list，计算取出顺序\n",
    "        n = len(deck)\n",
    "        d = deque(deck)\n",
    "        shunxu = {}\n",
    "        idx = 0\n",
    "        while d:\n",
    "            # 取第一张\n",
    "            num = d.popleft()\n",
    "            shunxu[num] = idx\n",
    "            idx += 1\n",
    "            # 牌首放牌尾\n",
    "            if d:\n",
    "                d.append(d.popleft())\n",
    "        # 2.排序deck\n",
    "        tmp = sorted(deck)\n",
    "        return [tmp[shunxu[deck[i]]] for i in range(n)]\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        result = []\n",
    "        deck.sort()\n",
    "        count = len(deck)\n",
    "        n = 1\n",
    "        while True:\n",
    "            if count+1 == n:\n",
    "                break\n",
    "            \n",
    "            result.insert(0, deck[-n])\n",
    "        \n",
    "        \n",
    "            if len(result) > 2:\n",
    "                result[:] = [result[0]] + [result[-1]] + result[1:-1]\n",
    "            n += 1\n",
    "\n",
    "        return result\n",
    " \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        n=len(deck)\n",
    "        d=collections.deque(range(n))\n",
    "        ans=[0]*n\n",
    "        for c in sorted(deck):\n",
    "            ans[d.popleft()]=c\n",
    "            if d:\n",
    "                d.append(d.popleft())\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        LEN = len(deck)\n",
    "        index = collections.deque(range(LEN)) # [0 1 2 3 4...]\n",
    "        \n",
    "        ans = [None] * LEN\n",
    "        for card in sorted(deck):\n",
    "            ans[index.popleft()] = card\n",
    "            if index:\n",
    "                index.append(index.popleft())\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        qq=sorted(deck)\n",
    "        ans=[i for i in range(len(deck))]\n",
    "        pp=deque(ans)\n",
    "        ll=0\n",
    "        while ll<len(deck):\n",
    "            l=pp.popleft()\n",
    "            if not pp:\n",
    "                ans[l]=qq[ll]\n",
    "                ll+=1\n",
    "                break\n",
    "            r=pp.popleft()\n",
    "            ans[l]=qq[ll]\n",
    "            ll+=1\n",
    "            pp.append(r)\n",
    "        return ans\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort(reverse=True)\n",
    "        ans=[]\n",
    "        def move_right(nums):\n",
    "            if not nums:\n",
    "                return nums\n",
    "            temp=nums[-1]\n",
    "            for i in range(len(nums)-1,0,-1):\n",
    "                nums[i]=nums[i-1]\n",
    "            nums[0]=temp\n",
    "            return nums\n",
    "        for i,num in enumerate(deck):\n",
    "            ans=move_right(ans)\n",
    "            ans.insert(0,num)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        q = collections.deque(list(range(len(deck))))\n",
    "        res = [0 for _ in range(len(deck))]\n",
    "        deck.sort()\n",
    "        for card in deck:\n",
    "            res[q.popleft()] = card\n",
    "            if q:\n",
    "                q.append(q.popleft())\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        # 要找到满足模拟的位次，对位次显示的顺序模拟，得0246315，按这个位次显示2、3、5、7、11、13、17\n",
    "        # 0 - 第1位后延 - 2 - 第3位后延 - 4 - 第5位后延 - 6 - 第1位后延 - 3 - 第5位后延 - 1 - 最后第5位\n",
    "        # 2 - 跳过一张  - 3 - 跳过一张  - 5 - 跳过一张  - 7 - 跳过一张  - 11 - 跳过一张 - 13 - 最后一张17\n",
    "        n = list(range(len(deck)))\n",
    "        m = [0]*len(deck)\n",
    "        ans = 0\n",
    "        while ans < len(deck):\n",
    "            m[ans] = n[0]\n",
    "            n.pop(0)\n",
    "            if n:\n",
    "                n.append(n[0])\n",
    "                n.pop(0)\n",
    "            ans += 1\n",
    "        deck.sort()\n",
    "        c = [0]*len(deck)\n",
    "        for i in range(len(deck)):\n",
    "            c[m[i]] = deck[i] \n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort()\n",
    "        res = []\n",
    "        for _ in range(len(deck)):\n",
    "            res.insert(0, deck.pop())\n",
    "            res.insert(0, res.pop())\n",
    "        res.append(res.pop(0))\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort()\n",
    "        res = []\n",
    "        for _ in range(len(deck)):\n",
    "            res.insert(0, deck.pop())\n",
    "            res.insert(0, res.pop())\n",
    "        res.append(res.pop(0))\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort(reverse=True)\n",
    "\n",
    "        dq = deque()\n",
    "        for card in deck:\n",
    "            if dq:\n",
    "                dq.appendleft(dq.pop())\n",
    "            dq.appendleft(card)\n",
    "        \n",
    "        return list(dq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort()\n",
    "        ret = [0] * len(deck)\n",
    "        index = deque(range(len(deck)))\n",
    "        for i in sorted(deck):\n",
    "            ret[index.popleft()] = i\n",
    "            if index:\n",
    "                index.append(index.popleft())\n",
    "        return ret\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def deckRevealedIncreasing(self, deck):\n",
    "        deck.sort(reverse=True)\n",
    "        ret = collections.deque()\n",
    "        for i in range(len(deck)):\n",
    "            if ret:\n",
    "                x = ret.pop()\n",
    "                ret.extendleft([x, deck[i]])\n",
    "            else:\n",
    "                ret.append(deck[i])\n",
    "        return list(ret)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort()\n",
    "        slen=len(deck)\n",
    "        res=[]\n",
    "        for i in range(slen-1,0,-1):\n",
    "            res.append(deck[i])\n",
    "            res.append(res[0])\n",
    "            res.pop(0)\n",
    "            \n",
    "        res.append(deck[0])\n",
    "        return res[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort()\n",
    "        n = len(deck)\n",
    "        res = [0] * n\n",
    "        d =deque(range(n))\n",
    "        for i in deck:\n",
    "            res[d.popleft()] = i\n",
    "            if d:\n",
    "                d.append(d.popleft())\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort()\n",
    "        n = len(deck)\n",
    "        res = [0] * n\n",
    "        indexs = deque(range(n))\n",
    "        for d in deck:\n",
    "            res[indexs.popleft()] = d \n",
    "            if indexs:\n",
    "                indexs.append(indexs.popleft())\n",
    "        return res\n",
    "\n",
    "        \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        # deck.sort(reverse=True)\n",
    "        # from collections import deque\n",
    "        # q=deque()\n",
    "        # # print(deck)\n",
    "        # for x in deck:\n",
    "        #     q.rotate()\n",
    "        #     q.appendleft(x)\n",
    "        #     # print(q)\n",
    "        # return list(q)\n",
    "\n",
    "\n",
    "\n",
    "        deck.sort()\n",
    "        ret = [0] * len(deck)\n",
    "        index = deque(range(len(deck)))\n",
    "        for i in sorted(deck):\n",
    "            ret[index.popleft()] = i\n",
    "            if index:\n",
    "                index.append(index.popleft())\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        n = len(deck)\n",
    "        index = list(range(n))\n",
    "        res = [0] * n\n",
    "\n",
    "        for card in sorted(deck):\n",
    "            res[index.pop(0)] = card\n",
    "\n",
    "            if index:\n",
    "                index.append(index.pop(0))\n",
    "        return res\n",
    "\n",
    "\n",
    "# class Solution:\n",
    "#     def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "#         check = sorted(deck, reverse=True)\n",
    "#         # print(check)\n",
    "#         newCheck = []\n",
    "#         while check:\n",
    "#             i = check.pop(0)\n",
    "#             # print(i)\n",
    "#             newCheck.insert(0, i)\n",
    "#             if check and len(newCheck) > 1:\n",
    "#                 j = newCheck.pop()\n",
    "#                 newCheck.insert(0, j)\n",
    "#         return newCheck\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        res:List = []\n",
    "        deck.sort()\n",
    "        for i in reversed(range(len(deck))):\n",
    "            if len(res) == 0:\n",
    "                res.append(deck[i])\n",
    "                continue\n",
    "            last = res.pop()\n",
    "            res.insert(0,last)\n",
    "            res.insert(0,deck[i])\n",
    "        \n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck = sorted(deck)\n",
    "        newdeck = []\n",
    "        for i in range(len(deck)):\n",
    "            deck_max = deck[len(deck) - i -1]\n",
    "            newdeck = [deck_max] + newdeck\n",
    "            newdeck = [newdeck[-1]] + newdeck[:-1] \n",
    "        newdeck = newdeck[1:] + [newdeck[0]]\n",
    "        return newdeck"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        # 对牌组进行排序\n",
    "        deck.sort()\n",
    "        \n",
    "        # 初始化一个空的队列，用于模拟抽牌过程\n",
    "        queue = []\n",
    "        for i in range(len(deck)):\n",
    "            queue.append(i)\n",
    "        \n",
    "        # 用于存储最终结果的数组\n",
    "        result = [0] * len(deck)\n",
    "        \n",
    "        for card in deck:\n",
    "            # 第一步：抽一张牌，将其放入结果数组中\n",
    "            result[queue.pop(0)] = card\n",
    "            \n",
    "            # 第二步：如果队列中仍有牌，将下一张牌移到队列末尾\n",
    "            if queue:\n",
    "                queue.append(queue.pop(0))\n",
    "        \n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        qq=sorted(deck)\n",
    "        ans=[i for i in range(len(deck))]\n",
    "        pp=deque(ans)\n",
    "        aa=[0 for i in range(len(deck))]\n",
    "        ll=0\n",
    "        while ll<len(deck):\n",
    "            l=pp.popleft()\n",
    "            if not pp:\n",
    "                ans[l]=qq[ll]\n",
    "                ll+=1\n",
    "                break\n",
    "            r=pp.popleft()\n",
    "            ans[l]=qq[ll]\n",
    "            ll+=1\n",
    "            pp.append(r)\n",
    "        return ans\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort()\n",
    "        res:List = []\n",
    "        for i in reversed(range(len(deck))):\n",
    "            if len(res) == 0:\n",
    "                res.append(deck[i])\n",
    "                continue\n",
    "            res.insert(0,res.pop())\n",
    "            res.insert(0,deck[i])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        LEN = len(deck)\n",
    "        index = collections.deque(range(LEN)) # [0 1 2 3 4...]\n",
    "        \n",
    "        ans = [None] * LEN\n",
    "        for card in sorted(deck):\n",
    "            ans[index.popleft()] = card\n",
    "            if index:\n",
    "                index.append(index.popleft())\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort()\n",
    "        res = []\n",
    "        for _ in range(len(deck)):\n",
    "            res.insert(0, deck.pop())\n",
    "            res.insert(0, res.pop())\n",
    "        res.append(res.pop(0))\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import *\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        n = len(deck)\n",
    "        deck.sort()\n",
    "        indexQueue = Deque([i for i in range(n)])\n",
    "        ans = [-1] * n\n",
    "        for v in deck:\n",
    "            i = indexQueue.popleft()\n",
    "            ans[i] = v\n",
    "            if indexQueue:\n",
    "                indexQueue.append(indexQueue.popleft())\n",
    "            \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        heap_curr = [-temp for temp in deck]#.copy()\n",
    "        heapq.heapify(heap_curr)\n",
    "        out = deque()\n",
    "\n",
    "        while heap_curr:\n",
    "            temp_min = heapq.heappop(heap_curr)\n",
    "            if out:\n",
    "                temp2 = out.pop()\n",
    "                out.appendleft(temp2)\n",
    "                out.appendleft(temp_min)\n",
    "            else:\n",
    "                out.append(temp_min)\n",
    "\n",
    "        return [-temp for temp in out]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        res = [0]*len(deck)\n",
    "        deck = sorted(deck)\n",
    "        index = [i for i in range(len(deck))]\n",
    "        for i in deck:\n",
    "            res[index[0]] = i\n",
    "            del index[0]\n",
    "            if index:\n",
    "                index.append(index[0])\n",
    "                del index[0]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        N = len(deck)\n",
    "        index = collections.deque(range(N))\n",
    "        ans = [None] * N\n",
    "\n",
    "        for card in sorted(deck):\n",
    "            ans[index.popleft()] = card\n",
    "            if index:\n",
    "                index.append(index.popleft())\n",
    "\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        n = len(deck)\n",
    "        arr = list(range(n))\n",
    "        ans = []\n",
    "        while arr:\n",
    "            if arr: ans.append(arr.pop(0))\n",
    "            if arr: arr.append(arr.pop(0))\n",
    "        deck.sort()\n",
    "        res = [0]*n\n",
    "        for i in range(n):\n",
    "            res[ans[i]] = deck[i]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        n = len(deck)\n",
    "        q = deque(range(n))\n",
    "        deck.sort()\n",
    "        ans = [0]*n\n",
    "        for x in deck:\n",
    "            ans[q.popleft()] = x\n",
    "            q.rotate(-1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import deque\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        n_list = deque()\n",
    "        for i in range(len(deck)):\n",
    "            n_list.append(i)\n",
    "        res = []\n",
    "        res_ = [0]*len(deck)\n",
    "        while n_list:\n",
    "            i = n_list.popleft()\n",
    "            res.append(i)\n",
    "            if n_list:\n",
    "                j = n_list.popleft()\n",
    "                n_list.append(j)\n",
    "        deck.sort()\n",
    "        for i in range(len(deck)):\n",
    "            res_[res[i]] = deck[i]\n",
    "        return res_\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        s1 = [i for i in range(len(deck))]\n",
    "        s2 = []\n",
    "        while s1:\n",
    "            s2.append(s1.pop(0))\n",
    "            if s1:\n",
    "                t = s1.pop(0)\n",
    "                s1.append(t)\n",
    "        deck_sort =[x for _,x in sorted(zip(s2,sorted(deck)))]\n",
    "        return deck_sort \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck = sorted(deck)\n",
    "        n = len(deck)\n",
    "        quene = deque()\n",
    "        for i in range(n - 1, -1, -1):\n",
    "            quene.append(deck[i])\n",
    "            if i == 0:\n",
    "                break \n",
    "            num = quene.popleft()\n",
    "            quene.append(num)\n",
    "        \n",
    "        quene = list(quene)[::-1]\n",
    "        return quene"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        sortd = sorted(deck)\n",
    "        res = [0] * len(deck)\n",
    "        que = [i for i in range(len(deck))]\n",
    "        indexes = [0] * len(deck)\n",
    "        cnt = 0\n",
    "        while cnt < len(sortd):\n",
    "            indexes[cnt] = que.pop(0)\n",
    "            cnt += 1\n",
    "            if que:\n",
    "                top = que.pop(0)\n",
    "                que.append(top)\n",
    "        for i in range(len(deck)):\n",
    "            index = indexes[i]\n",
    "            res[index] = sortd[i]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        qq=sorted(deck)\n",
    "        ans=[i for i in range(len(deck))]\n",
    "        pp=deque(ans)\n",
    "        aa=[0 for i in range(len(deck))]\n",
    "        res=[]\n",
    "        ll=0\n",
    "        while ll<len(deck):\n",
    "            l=pp.popleft()\n",
    "            if not pp:\n",
    "                res.append(l)\n",
    "                aa[l]=qq[ll]\n",
    "                ll+=1\n",
    "                break\n",
    "            r=pp.popleft()\n",
    "            res.append(l)\n",
    "            aa[l]=qq[ll]\n",
    "            ll+=1\n",
    "            pp.append(r)\n",
    "        return aa\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import queue\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        deck.sort(reverse = True)\n",
    "        q = queue.deque()\n",
    "        for i in range(len(deck)):\n",
    "            if not q: q.append(deck[i])\n",
    "            else:\n",
    "                q.appendleft(q.pop())\n",
    "                q.appendleft(deck[i])\n",
    "        return list(q)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        res = self.digui(deck, 1)\n",
    "        # if len(res) % 2 == 1:\n",
    "        #     res_out = res[1:]\n",
    "        #     res_out.append(res[0])\n",
    "        # else:\n",
    "        #     res_out = res\n",
    "        return res\n",
    "    \n",
    "    def digui(self, nums, jiou):\n",
    "        print(nums)\n",
    "        if len(nums) == 1:\n",
    "            return nums\n",
    "        if len(nums)  == 2:\n",
    "            if jiou:\n",
    "                nums.sort()\n",
    "                return nums\n",
    "            else:\n",
    "                nums.sort(reverse = True)\n",
    "                return nums\n",
    "        nums.sort()\n",
    "        ou = nums[:ceil(len(nums) / 2)]\n",
    "        ji = self.digui(nums[ceil(len(nums) / 2):], len(nums) % 2 == 0)\n",
    "        i = 0\n",
    "        res = []\n",
    "        while i < len(ou):\n",
    "            res.append(ou[i])\n",
    "            if i < len(ji):\n",
    "                res.append(ji[i])\n",
    "            i += 1\n",
    "        if not jiou:\n",
    "            res_out = [res[-1]]\n",
    "            res_out.extend(res[:-1])\n",
    "        else:\n",
    "            res_out = res\n",
    "        return res_out\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, input: List[int]) -> List[int]:\n",
    "        nums = input\n",
    "        if len(nums) == 1: return nums\n",
    "        elif len(nums) == 2:\n",
    "            nums.sort()\n",
    "            return nums\n",
    "        tmp = [i for i in nums]\n",
    "        nums.sort()\n",
    "        ans = []\n",
    "        n = len(nums)\n",
    "        idx = n - 3\n",
    "        ans = [nums[-2],nums[-1]]\n",
    "        state = 0\n",
    "        while idx >= 0:\n",
    "            ans.insert(0,nums[idx])\n",
    "            t = ans[-1]\n",
    "            ans.pop(-1)\n",
    "            ans.insert(1,t)\n",
    "            idx -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        \n",
    "        n = len(deck)\n",
    "        ans = [None]*n\n",
    "        index = [i for i in range(n)]\n",
    "        \n",
    "        print(index)\n",
    "        for card in sorted(deck):\n",
    "            ans[index.pop(0)] = card\n",
    "            if index:\n",
    "                index.append(index.pop(0))\n",
    "        \n",
    "        return ans\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:\n",
    "        s_deck = sorted(deck) # 2 3 5  7 11 13 17\n",
    "        temp = [i for i in range(len(deck))] #剩余 [0,1 ,2 3 4 5 6]\n",
    "        re = [0] * len(deck)\n",
    "        j = 0\n",
    "        jishu = True\n",
    "        while j < len(s_deck): \n",
    "            temp_c = temp.copy()\n",
    "            c_jishu = True\n",
    "            if len(temp) == 1:\n",
    "                re[temp[0]] = s_deck[j]\n",
    "                temp_c.remove(temp[0])\n",
    "                j += 1\n",
    "            else:    \n",
    "                for i in range(len(temp)):\n",
    "                    if jishu and i % 2 == 0:\n",
    "                        re[temp[i]] = s_deck[j]\n",
    "                        temp_c.remove(temp[i])\n",
    "                        j += 1\n",
    "                        if i == len(temp) - 1:\n",
    "                            c_jishu = False\n",
    "                    elif not jishu and i % 2 == 1:\n",
    "                        re[temp[i]] = s_deck[j]\n",
    "                        temp_c.remove(temp[i])\n",
    "                        j += 1\n",
    "                        if i == len(temp) - 1:\n",
    "                            c_jishu = False     \n",
    "            temp = temp_c\n",
    "            if not c_jishu:\n",
    "                jishu = False\n",
    "            else:\n",
    "                jishu = True\n",
    "        return re\n",
    "            \n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
